//npm install mysql
const { profile } = require('console')
const mysql = require('mysql');
const { resolve } = require('path/posix');

//创建连接对象
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    // password: 'root',
    password: 'hwj99999',
    port: 3306,
    database: 'profile'
})

//开始连接
connection.connect();

//执行sql语句
// const sql = `select * from registered;`
// connection.query(sql, (err, result) => {
//     if (err) {
//         console.log('error', err);
//         return

//     }
//     console.log('result', result);

// })

//执行sql语句
// function execSQL(sql, callback) {
//     connection.query(sql, callback)
// }

function execSQL(sql) {
    const promise = new Promise((resolve, reject) => {
        connection.query(sql, (err, result) => {
            if (err) {
                reject(err);
                return

            }
            resolve(result)
            // console.log('result', result);

        })
    })

    return promise;
}

//导出execSQL
module.exports = {
    execSQL
}